package com.zlyx.easy.swagger.utils;

import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.isNullOrEmpty;
import static springfox.documentation.schema.Types.typeNameFor;

import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.ModelReference;

/**
 * @Auth 赵光
 * @Describle
 * @2019年6月27日 下午7:53:29
 */
public class ModelRefUtils {

	public static ModelReference getModelRef(String typeName, String container) {
		ModelReference modelReference;
		if (isNullOrEmpty(container)) {
			modelReference = new ModelRef(typeName);
		} else {
			modelReference = new ModelRef(container, getModelRef(typeName));
		}
		return modelReference;
	}

	public static ModelReference getModelRef(String typeName) {
		return getModelRef(typeName, null);
	}

	public static ModelReference getModelRef(Class<?> typeClass, String container) {
		ModelReference modelReference;
		String typeName = fromNullable(typeNameFor(typeClass)).or("string");
		if (isNullOrEmpty(container)) {
			modelReference = new ModelRef(typeName);
		} else {
			modelReference = new ModelRef(container, getModelRef(typeClass));
		}
		return modelReference;
	}

	public static ModelReference getModelRef(Class<?> typeClass) {
		return getModelRef(typeClass, null);
	}
}
